# -*- coding: utf-8 -*-
# article_info.py
# Created by Hardy on 26th, Jan
# Copyright 2017 杭州网川教育有限公司. All rights reserved.


from querier.esquerier import ElasticSearchQuerier
import psycopg2


class MovieInfoQuerier(ElasticSearchQuerier):

    def __init__(self, article_search, config):
        super(MovieInfoQuerier, self).__init__(None, None, None)
        self.article_search = article_search
        self.host = config['host']
        self.port = config['port']
        self.user = config['user']
        self.password = config['password']
        self.db = config['database']
        # self.table = config['table']

    def search(self, args):
        arg_dict = args.get('arg_dict')
        arg_dict = {} if not arg_dict else arg_dict

        offset = arg_dict.get('offset', 0)
        limit = arg_dict.get('limit', 10)

        term = args.get('term', '')
        conn, cur = None, None

        res = []

        try:
            conn = psycopg2.connect(database=self.db, user=self.user, password=self.password,
                                    host=self.host, port=self.port)

            cur = conn.cursor()

            sql = """select 
                     name, 
                     ename,
                     other_name,
                     produced_time,
                     company,
                     area,
                     director,
                     screenwriter,
                     producer,
                     type,
                     starring_actor,
                     length,
                     language,
                     introduction,
                     img,
                     additional,
                     info,
                     roles,
                     broadcast_time 
                     from movie_baike where name like '%s'
                     order by produced_time desc, additional desc limit %s offset %s"""
            SQL = sql % ('%' + term + '%', limit, offset)
            cur.execute(SQL)
            for d in cur.fetchall():
                print(d)
                res.append({
                    'title': d[0],
                    'ename': d[1],
                    'other_name': d[2],
                    'produced_time': d[3],
                    'company': d[4],
                    'area': d[5],
                    'director': d[6],
                    'screenwriter': d[7],
                    'producer': d[8],
                    'type': d[9],
                    'starring_actor': d[10],
                    'length': d[11],
                    'language': d[12],
                    'introduction': d[13],
                    'img': d[14],
                    'additional': d[15],
                    'info': d[16],
                    'roles': d[17],
                    'broadcast_time': d[18],
                })

        except Exception as e:
            print(str(e))
        finally:
            if conn:
                conn.close()

        result = []
        for d in res:
            title = d['title']
            result.append({
                'title': title,
                'info': d
            })

        return {
            "result": result
        }

    def _build_query(self, args):
        pass

    def _build_result(self, result, param):
        pass
